//
//  XLMatchPopUpView.m
//  XiangLove
//
//  Created by 陈宪栋 on 2018/3/7.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLMatchPopUpView.h"

@interface XLMatchPopUpView ()
@property (nonatomic, strong) CAShapeLayer *backgroundLayer;
@property (nonatomic, strong) UILabel *textLabel;
@end

@implementation XLMatchPopUpView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _cornerRadius = 17.5;
        _arrowSize = CGSizeMake(23, 15);
        
        _backgroundLayer = [CAShapeLayer layer];
        _backgroundLayer.fillColor = RGBA(255, 255, 255, 0.28).CGColor;
        [self.layer addSublayer:_backgroundLayer];
    }
    return self;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    [self setNeedsLayout];
}

- (UILabel *)textLabel {
    if (!_textLabel) {
        _textLabel = [[UILabel alloc] init];
        _textLabel.font = [UIFont systemFontOfSize:14];
        _textLabel.textColor = [UIColor whiteColor];
        _textLabel.numberOfLines = 1;
        [self addSubview:_textLabel];
    }
    return _textLabel;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGSize arrowSize = _arrowSize;
    CGFloat cornerRadius = _cornerRadius;
    CGFloat arrowMinX = self.frame.size.width / 2 - arrowSize.width / 2;
    
    CGRect roundedRect = CGRectMake(0, arrowSize.height, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) - arrowSize.height);
    CGPoint leftTopArcCenter = CGPointMake(CGRectGetMinX(roundedRect) + cornerRadius, CGRectGetMinY(roundedRect) + cornerRadius);
    CGPoint leftBottomArcCenter = CGPointMake(leftTopArcCenter.x, CGRectGetMaxY(roundedRect) - cornerRadius);
    CGPoint rightTopArcCenter = CGPointMake(CGRectGetMaxX(roundedRect) - cornerRadius, leftTopArcCenter.y);
    CGPoint rightBottomArcCenter = CGPointMake(rightTopArcCenter.x, leftBottomArcCenter.y);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:self.frame.origin];
    
    [path moveToPoint:CGPointMake(leftTopArcCenter.x, CGRectGetMinY(roundedRect))];
    [path addArcWithCenter:leftTopArcCenter radius:cornerRadius startAngle:M_PI * 1.5 endAngle:M_PI clockwise:NO];
    [path addLineToPoint:CGPointMake(CGRectGetMinX(roundedRect), leftBottomArcCenter.y)];
    [path addArcWithCenter:leftBottomArcCenter radius:cornerRadius startAngle:M_PI endAngle:M_PI * 0.5 clockwise:NO];
    
    [path addLineToPoint:CGPointMake(rightBottomArcCenter.x, CGRectGetMaxY(roundedRect))];
    [path addArcWithCenter:rightBottomArcCenter radius:cornerRadius startAngle:M_PI * 0.5 endAngle:0.0 clockwise:NO];
    [path addLineToPoint:CGPointMake(CGRectGetMaxX(roundedRect), rightTopArcCenter.y)];
    [path addArcWithCenter:rightTopArcCenter radius:cornerRadius startAngle:0.0 endAngle:M_PI * 1.5 clockwise:NO];
    
    [path addLineToPoint:CGPointMake(arrowMinX + arrowSize.width, CGRectGetMinY(roundedRect))];
    [path addLineToPoint:CGPointMake(arrowMinX + arrowSize.width / 2, CGRectGetMinY(roundedRect) - arrowSize.height)];
    [path addLineToPoint:CGPointMake(arrowMinX, CGRectGetMinY(roundedRect))];
    
    [path closePath];
    
    _backgroundLayer.path = path.CGPath;
    _backgroundLayer.shadowPath = path.CGPath;
    _backgroundLayer.frame = self.bounds;
    
    CGSize textSize = [self.textLabel sizeThatFits:CGSizeMake(CGRectGetWidth(self.frame) - 2 * cornerRadius, CGRectGetHeight(self.frame) - arrowSize.height)];
    _textLabel.size = textSize;
    _textLabel.center = CGPointMake(CGRectGetWidth(self.frame) / 2, arrowSize.height + (CGRectGetHeight(self.frame) - arrowSize.height) / 2);
}

@end
